﻿Public Class Configuration

    Public Shared id As Decimal

#Region "Package"

    Public Shared Function AllPackage() As List(Of Entity.PackageItem)
        Return DL.Package.AllPackage
    End Function

    Public Shared Function GetPackage(ByVal info As Entity.PackageItem) As Data.DataTable
        Return DL.Package.GetPackage(info)
    End Function

    Public Shared Function GetPackageById(ByVal info As String) As Entity.PackageItem
        Return DL.Package.GetPackageById(info)
    End Function

    Public Shared Function InsertPackage(ByVal info As Entity.PackageItem) As Boolean
        Dim da As New DL.Package(False)
        Return da.Insert(info)
    End Function

    Public Shared Function UpdatePackage(ByVal info As Entity.PackageItem) As Boolean
        Dim rslt As Boolean
        rslt = True
        Dim da As New DL.Package(True)
        Try
            rslt = da.InsertHistory(GetPackageById(info.package_id))
            If rslt Then
                rslt = da.Update(info)
            End If

            If rslt Then
                da.Commit()
                Return True
            Else
                da.RollBack()
                Return False
            End If
        Catch ex As Exception
            Try
                da.RollBack()
            Catch
            End Try
            Throw ex
        End Try
    End Function

#End Region

#Region "RV"

    Public Shared Function GetRV(ByVal info As Entity.RVItem) As Data.DataTable
        Return DL.RV.GetRV(info)
    End Function

    Public Shared Function GetRVById(ByVal info As Decimal) As Entity.RVItem
        Return DL.RV.GetRVById(info)
    End Function

    Public Shared Function InsertRV(ByVal info As Entity.RVItem, ByVal infodetail As DataTable) As Boolean
        Dim rslt As Boolean
        rslt = True
        Dim daoRV As New DL.RV(True)

        Try
            id = daoRV.GetRVNewID()
            info.calculate_rv_id = id
            rslt = daoRV.Insert(info)

            If rslt Then
                For Each obj As DataRow In infodetail.Rows
                    rslt = daoRV.InsertDetail(id, obj)
                    If Not rslt Then Exit For
                Next
            End If

            If rslt Then
                daoRV.Commit()
                Return True
            Else
                daoRV.RollBack()
                Return False
            End If
        Catch ex As Exception
            Try
                daoRV.RollBack()
            Catch
            End Try
            Throw ex
        End Try
    End Function

    Public Shared Function UpdateRV(ByVal info As Entity.RVItem, ByVal infodetail As DataTable) As Boolean
        Dim rslt As Boolean
        rslt = True
        Dim daoRV As New DL.RV(True)

        Try
            id = info.calculate_rv_id
            rslt = daoRV.InsertHistory(GetRVById(id))

            Dim hisid As Decimal = DL.RV.hisid

            If rslt Then
                Dim dthis As DataTable = New DataTable
                dthis = GetRVDetail(id)

                For Each obj As DataRow In dthis.Rows
                    rslt = daoRV.InsertDetailHistory(hisid, id, obj)
                    If Not rslt Then Exit For
                Next
            End If

            If rslt Then rslt = daoRV.Update(info)
            If rslt Then rslt = daoRV.DeleteDetail(id)

            If rslt Then
                For Each obj As DataRow In infodetail.Rows
                    rslt = daoRV.InsertDetail(id, obj)
                    If Not rslt Then Exit For
                Next
            End If

            If rslt Then
                daoRV.Commit()
                Return True
            Else
                daoRV.RollBack()
                Return False
            End If
        Catch ex As Exception
            Try
                daoRV.RollBack()
            Catch
            End Try
            Throw ex
        End Try
    End Function

#End Region

#Region "RV Detail"

    Public Shared Function GetRVDetail(ByVal infoid As Decimal) As Data.DataTable
        Return DL.RV.GetRVDetail(infoid)
    End Function

#End Region

#Region "Sponsor"

    Public Shared Function GetSponsor(ByVal info As Entity.SponsorItem) As Data.DataTable
        Return DL.Sponsor.GetSponsor(info)
    End Function

    Public Shared Function GetSponsorById(ByVal info As Decimal) As Entity.SponsorItem
        Return DL.Sponsor.GetSponsorById(info)
    End Function

    Public Shared Function InsertSponsor(ByVal info As Entity.SponsorItem) As Boolean
        Dim da As New DL.Sponsor(False)
        id = da.GetNewID
        info.sponsor_id = id
        Return da.Insert(info)
    End Function

    Public Shared Function UpdateSponsor(ByVal info As Entity.SponsorItem) As Boolean
        Dim rslt As Boolean
        rslt = True
        Dim da As New DL.Sponsor(True)
        Try
            rslt = da.InsertHistory(GetSponsorById(info.sponsor_id))
            If rslt Then
                rslt = da.Update(info)
            End If

            If rslt Then
                da.Commit()
                Return True
            Else
                da.RollBack()
                Return False
            End If
        Catch ex As Exception
            Try
                da.RollBack()
            Catch
            End Try
            Throw ex
        End Try

    End Function

#End Region

#Region "Point"

    Public Shared Function GetPoint(ByVal info As Entity.PointItem) As Data.DataTable
        Return DL.Point.GetPoint(info)
    End Function

    Public Shared Function GetPointById(ByVal info As Decimal) As Entity.PointItem
        Return DL.Point.GetPointById(info)
    End Function

    Public Shared Function InsertPoint(ByVal info As Entity.PointItem) As Boolean
        Dim da As New DL.Point(False)
        id = da.GetNewID
        info.point_id = id
        Return da.Insert(info)
    End Function

    Public Shared Function UpdatePoint(ByVal info As Entity.PointItem) As Boolean
        Dim rslt As Boolean
        rslt = True
        Dim da As New DL.Point(True)
        Try
            rslt = da.InsertHistory(GetPointById(info.point_id))
            If rslt Then
                rslt = da.Update(info)
            End If

            If rslt Then
                da.Commit()
                Return True
            Else
                da.RollBack()
                Return False
            End If
        Catch ex As Exception
            Try
                da.RollBack()
            Catch
            End Try
            Throw ex
        End Try
    End Function

#End Region

End Class
